local extension = Package("jlve_star")
extension.extensionName = "jlve"

Fk:loadTranslationTable{
    ["jlve_sp"] = "极略SP",
}

local U = require "packages/utility/utility"
local T = require "packages/jlve/record/Theorem"

local fanghun = fk.CreateViewAsSkill{
    name = "jlve__fanghun",
    pattern = "slash,jink",
    card_filter = function(self, to_select, selected)
      if #selected == 1 then return false end
      local _c = Fk:getCardById(to_select)
      local c
      if _c.trueName == "slash" then
        c = Fk:cloneCard("jink")
      elseif _c.name == "jink" then
        c = Fk:cloneCard("slash")
      else
        return false
      end
      return (Fk.currentResponsePattern == nil and Self:canUse(c)) or (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(c))
    end,
    view_as = function(self, cards)
      if #cards ~= 1 then
        return nil
      end
      local _c = Fk:getCardById(cards[1])
      local c
      if _c.trueName == "slash" then
        c = Fk:cloneCard("jink")
      elseif _c.name == "jink" then
        c = Fk:cloneCard("slash")
      end
      c.skillName = self.name
      c:addSubcard(cards[1])
      return c
    end,
}
local fanghun_trigger = fk.CreateTriggerSkill{
    name = "#jlve__fanghun_trigger",
    anim_type = "offensive",
    events = {fk.CardUsing, fk.CardResponding},
    can_trigger = function(self, event, target, player, data)
      if target == player and player:hasSkill(self) and
        table.find(data.card.skillNames, function(name) return string.find(name, "jlve__fanghun") end) then
        local id
        if event == fk.CardUsing then
          if data.card.trueName == "slash" then
            id = data.tos[1][1]
          elseif data.card.name == "jink" then
            if data.responseToEvent then
              id = data.responseToEvent.from  --jink
            end
          end
        elseif event == fk.CardResponding then
          if data.responseToEvent then
            if data.responseToEvent.from == player.id then
              id = data.responseToEvent.to  --duel used by zhaoyun
            else
              id = data.responseToEvent.from  --savsavage_assault, archery_attack, passive duel
  
              --TODO: Lenovo shu zhaoyun may chongzhen liubei when responding to jijiang
            end
          end
        end
        if id ~= nil then
          self.cost_data = id
          return not player.room:getPlayerById(id):isKongcheng()
        end
      end
    end,
    on_cost = function(self, event, target, player, data)
      return player.room:askForSkillInvoke(player, self.name, nil, "#jlve__fanghun-invoke::"..self.cost_data)
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local to = room:getPlayerById(self.cost_data)
      local card = room:askForCardChosen(player, to, "h", self.name)
      room:obtainCard(player.id, card, false, fk.ReasonPrey)
    end,
}
fanghun:addRelatedSkill(fanghun_trigger)
Fk:loadTranslationTable{
    ["jlve__fanghun"] = "芳魂",
    ["#jlve__fanghun_trigger"] = "芳魂",
    [":jlve__fanghun"] = "你可以将【杀】当【闪】，【闪】当【杀】使用或打出，然后你可以立即获得对方的一张手牌。",
    ["#jlve__fanghun-invoke"] = "芳魂：你可以获得 %dest 的一张手牌",
  
    ["$jlve__fanghun1"] = "心如寒梅，不惧严霜。",
    ["$jlve__fanghun2"] = "芳华易逝，蜀魂不灭！",
}

local fuhan = fk.CreateTriggerSkill{
    name = "jlve__fuhan",
    events = {fk.CardUseFinished},
    --priority = 1,
    anim_type = "special",
    can_trigger = function(self, event, target, player, data)
      return  target == player and player:hasSkill(self)  and data.card and data.card:isVirtual() and data.card.subcards and #data.card.subcards > 0 and  player:usedSkillTimes(self.name, Player.HistoryTurn) == 0
    end,
    on_cost = function (self,event, target, player, data)
        return player.room:askForSkillInvoke(player,self.name,nil,"扶汉：你可以随机获得一个不在场上的蜀势力武将技能。")
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local genn = {}
      for _, p in ipairs(room.players) do
        table.insertIfNeed(genn,p.general)
        if p.deputyGeneral ~= "" then
            table.insertIfNeed(genn,p.deputyGeneral)
        end
      end
        local get = false
        local gp = table.simpleClone(Fk:getAllGenerals())
        table.shuffle(gp)
        for _, general in ipairs(gp) do
          if get then break end
          if (general.kingdom == "shu" or general.subkingdom == "shu") and not table.contains(genn,general.name) then
            local skills = table.simpleClone(general.skills)
            table.shuffle(skills)
            for _, s in ipairs(skills) do
              if get then break end
              if not (table.contains({Skill.Limited, Skill.Wake, Skill.Quest},s.frequency) or s.lordSkill) and not player:hasSkill(s,true) then
                get = true
                room:handleAddLoseSkills(player,s.name,nil,true)
              end
              if get then break end
            end
            if get then break end
          end
        end
    end,
}
Fk:loadTranslationTable{
    ["jlve__fuhan"] = "扶汉",
    [":jlve__fuhan"] = "每回合限一次，当你使用转化牌结算完成后，你可以随机获得一个不在场上的“蜀”势力武将牌的一个技能。",
  
    ["$jlve__fuhan1"] = "蜀汉的先辈们，请赐予我力量。",
    ["$jlve__fuhan2"] = "父亲，我会继承你的意志。",
}
local zhaoxiang = General(extension, "jlve__zhaoxiang","shu",4,4,General.Female)
zhaoxiang:addSkill(fanghun)
zhaoxiang:addSkill(fuhan)
Fk:loadTranslationTable{
    ["jlve__zhaoxiang"] = "赵襄",

    ["~jlve__zhaoxiang"] = "蜀汉的命运，真的无法改变吗？",
}



local caijian = fk.CreateActiveSkill{
  name = "jlve__caijian",
  anim_type = "masochism",
  target_num = 1,
  card_num = 0,
  prompt = "#jlve__caijian",
  can_use = function(self, player)
    local room = Fk:currentRoom()
    return   player:usedSkillTimes(self.name, Player.HistoryTurn) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected)
    local room = Fk:currentRoom()
    return #selected == 0 and not room:getPlayerById(to_select):isNude()
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local tar = room:getPlayerById(effect.tos[1])
    local card = room:askForCardChosen(player,tar,"he",self.name,"#jlve__caijian_choose")
    if card then
      local yes = false
      if Fk:getCardById(card).color == Card.Black then
        yes = true
      end
      if #player:getPile("jlve_jian") > 0 and yes then
        for _, cid in ipairs(player:getPile("jlve_jian")) do
          if Fk:getCardById(card).suit == Fk:getCardById(cid).suit then
            yes = false
          end
          if not yes then
            break
          end
        end
      end
      player:addToPile("jlve_jian",card, true, self.name)
      if yes then
        tar:turnOver()
      end
    end
  end,
}
local caijian_trigger = fk.CreateTriggerSkill{
  name = "#jlve__caijian_trigger",
  anim_type = "masochism",
  events = {fk.Damaged},
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    for _ = 1, data.damage do
      if player.dead or self.cancel_cost then return end
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local tar = {}
    for _, p in ipairs(player.room.alive_players) do
      if not p:isNude() then
        table.insert(tar,p.id)
      end
    end
    local targets = player.room:askForChoosePlayers(player,tar,1,1,"#jlve__caijian",self.name,true) 
    if #targets > 0 then 
      self.cost_data = targets[1]
      return true 
    end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tar = room:getPlayerById(self.cost_data)
    local card = room:askForCardChosen(player,tar,"he",self.name,"#jlve__caijian_choose")
    if card then
      local yes = false
      if Fk:getCardById(card).color == Card.Black then
        yes = true
      end
      if #player:getPile("jlve_jian") > 0 and yes then
        for _, cid in ipairs(player:getPile("jlve_jian")) do
          if Fk:getCardById(card).suit == Fk:getCardById(cid).suit then
            yes = false
          end
          if not yes then
            break
          end
        end
      end
      player:addToPile("jlve_jian",card, true, self.name)
      if yes then
        tar:turnOver()
      end
    end
  end
}
caijian:addRelatedSkill(caijian_trigger)
Fk:loadTranslationTable{
  ["jlve__caijian"] = "才鉴",
  [":jlve__caijian"] = "出牌阶段限一次，或每当你受到1点伤害后，你可以选择一名角色将其一张牌置于你的武将牌上，称为“鉴”;<br>若此“鉴”为黑色且与其他“鉴”花色不同，你令你选择的角色翻面。",
  ["#jlve__caijian"] = "你可以将一名角色的一张牌置入“鉴”。",
  ["#jlve__caijian_choose"] = "才鉴：选择一张牌。",
  ["#jlve__caijian_trigger"] = "才鉴",
  ["jlve_jian"] = "鉴",

  ["$jlve__caijian1"] = "以人为镜，可鉴忠良。",
  ["$jlve__caijian2"] = "狼子野心者，小女子亦能知晓。",
}

local zhishi = fk.CreateActiveSkill{
  name = "jlve__zhishi",
  anim_type = "special",
  card_num = 3,
  target_num = 0,
  expand_pile = "jlve_jian",
  can_use = function(self, player)
    return #player:getPile("jlve_jian") >= 3 
  end,
  target_filter = Util.FalseFunc,
  card_filter = function(self, to_select, selected)
    if #selected < 3 and Self:getPileNameOfId(to_select) ==  "jlve_jian" then
      if #selected == 0 then
        return true
      else
        return table.every(selected, function(id) return Fk:getCardById(to_select).suit ~= Fk:getCardById(id).suit end)
      end
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:moveCards({
      from = player.id,
      ids = effect.cards,
      toArea = Card.DiscardPile,
      moveReason = fk.ReasonPutIntoDiscardPile,
      skillName = self.name,
    })
    if player.dead then return end
    player:drawCards(3,self.name)
    if player.dead then return end
    local genn = {}
      for _, p in ipairs(room.players) do
        table.insertIfNeed(genn,p.general)
        if p.deputyGeneral ~= "" then
            table.insertIfNeed(genn,p.deputyGeneral)
        end
      end
        local get = false
        local gp = table.simpleClone(Fk:getAllGenerals())
        table.shuffle(gp)
        for _, general in ipairs(gp) do
          if get then break end
          if (general.kingdom == "wei" or general.subkingdom == "wei") and not table.contains(genn,general.name) then
            local skills = table.simpleClone(general.skills)
            table.shuffle(skills)
            for _, s in ipairs(skills) do
              if get then break end
              if not (table.contains({Skill.Limited, Skill.Wake, Skill.Quest},s.frequency) or s.lordSkill) and not player:hasSkill(s,true) then
                get = true
                room:handleAddLoseSkills(player,s.name,nil,true)
              end
              if get then break end
            end
            if get then break end
          end
        end
  end,
}
Fk:loadTranslationTable{
  ["jlve__zhishi"] = "智识",
  [":jlve__zhishi"] = "出牌阶段，你可以将三张花色各不相同的“鉴”置入弃牌堆，然后摸三张牌并随机获得一名不在场上的“魏”势力武将牌上的技能。",

  ["$jlve__zhishi1"] = "通今博古，慧眼识人。",
  ["$jlve__zhishi2"] = "明以料事，智以料人。",
}
local xinxianying = General(extension, "jlve__xinxianying","wei",3,3,General.Female)
xinxianying:addSkill(caijian)
xinxianying:addSkill(zhishi)
Fk:loadTranslationTable{
    ["jlve__xinxianying"] = "辛宪英",

    ["~jlve__xinxianying"] = "乱世乱料，神难自知。",
}

Fk:addQmlMark{
  name = "jlve_sanjue",
  qml_path = "packages/jlve/qml/Skillpool",
  how_to_show = function(name, value, p)
    -- FIXME: 神秘bug导致value可能为空串有待排查
    if type(value) == "table" then 
      return  tostring(#value)
    end
    return ""
  end,
 
}
local sanjue_tir = fk.CreateTriggerSkill{
  name = "#jlve__sanjue",
  events = {fk.CardUsing},
  frequency=Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and data.card then
      local card_suit = data.card.suit
      local room = player.room
      local logic = room.logic
      local card=data.card
      local n=0
        logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
          local use = e.data[1]
          if use.from and use.from==player.id and use.card.name == card.name  then
            n=n+1
          end
          return false
        end, Player.HistoryGame)

      return n==1 or n==3
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(1,self.name)
    local mark=type(player:getMark("@[jlve_sanjue]")) == "table" and player:getMark("@[jlve_sanjue]") or {}
    local existingGenerals = {}
    --筛选
    table.forEach(room.players, function(p)
      table.insertTable(existingGenerals, {p.general, p.deputyGeneral})
    end)
    local generals = table.map(Fk:getGeneralsRandomly(3, Fk:getAllGenerals(), existingGenerals,
    (function(p) return (p.kingdom ~="wu" or p.name == "mouxusheng") end)), function(g) return g.name end)
    --generals = table.random(generals, math.min(15, #generals))--武将筛选，此技能不需要
    local haveskils = {}
    for _, p in ipairs(room.alive_players) do
      for _, s in ipairs(p.player_skills) do
        table.insertIfNeed(haveskils,s.name)
      end
    end
    local skills = {}
    for _, general_name in ipairs(generals) do
    local general = Fk.generals[general_name]
  --加载技能
    for _, skill in ipairs(general.skills) do
    if not  skill.lordSkill and not table.contains(mark,skill.name) and not table.contains(haveskils,skill.name) then
      table.insertIfNeed(skills, skill.name)
    end
  end
    for _, s_name in ipairs(general.other_skills) do
    local skill = Fk.skills[s_name]
    if not  skill.lordSkill and not table.contains(mark,skill.name)  and not table.contains(haveskils,skill.name) then
      table.insertIfNeed(skills, skill.name)
    end
  end
  end
  if #skills>0 then
    local choice=table.random(skills,1)[1]
   table.insert(mark,choice)
    room:setPlayerMark(player,"@[jlve_sanjue]",mark)
  end
  end,

}
local sanjue=fk.CreateActiveSkill{
  name = "jlve__sanjue",
  anim_type = "offensive",
  prompt ="jlve-sanjue-give",
  can_use = function(self, player)
    return player.id > 0 and type(player:getMark("@[jlve_sanjue]")) == "table" and 
    #player:getMark("@[jlve_sanjue]")>0
  end,
  target_num = 1,
  target_filter = function(self, to_select, selected, selected_cards)
    return #selected == 0 
  end,
  card_filter  =  function(self, to_select, selected)
    return false
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local mark=player:getMark("@[jlve_sanjue]")
    room:askForCustomDialog(player, nil,
    "packages/utility/qml/ChooseSkillBox.qml", {
      mark, 1, 1, "jlve__sanjue-skill-choice"
    })
    if  player.reply_ready then
      local choice = json.decode(player.client_reply) 
      room:handleAddLoseSkills(target, table.concat(choice, "|"), nil, true,true)
      table.removeOne(mark,choice[1])
      if type(mark) == "table" then
      room:setPlayerMark(player,"@[jlve_sanjue]",mark)
      else
        room:setPlayerMark(player,"@[jlve_sanjue]",{})
      end
    end
  
    
  end,
}
sanjue:addRelatedSkill(sanjue_tir)

Fk:loadTranslationTable{
  ["@[jlve_sanjue]"] = "三绝储备",
  ["jlve_sanjue-prompt"] = "储备技能",
  ["jlve__sanjue"] = "三绝",
  ["#jlve__sanjue"] = "三绝",
  ["jlve-sanjue-give"] = "可以令一名角色获得一个储备技能",
  ["jlve__sanjue-skill-choice"] = "三绝：选择一个技能使其获得",
  [":jlve__sanjue"] = "锁定技，当你第一次或第三次使用同名牌时，你摸一张牌，然后从未在场上的吴势力技能中随机获得一个储备技能。出牌阶段，你可以令一名角色获得一个储备技能。",
  ["$jlve__sanjue1"] = "纺帛锦绣，五岳河海，此为臻绝。",
  ["$jlve__sanjue2"] = "大泽垠尺，小泽方寸，此为鸡绝。",
  ["$jlve__sanjue3"] = "薄如蝉翼，轻赛寒烟，此为丝绝。",
}
local zhaoyan = General(extension, "jlve__zhaoyan","wu",3,3,General.Female)
zhaoyan:addSkill(sanjue)
Fk:loadTranslationTable{
    ["jlve__zhaoyan"] = "赵嫣",

    ["~jlve__zhaoyan"] = "三绝之艺，终究是要失传了吗？",
}

local nanhualaoxian = General(extension, "jlve__nanhualaoxian","qun",3,3)
local xianshou = fk.CreateTriggerSkill{
  name = "jlve_xianshou",
  events = {fk.TurnStart},
  anim_type = "special",
  priority = 1.5,
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnStart then
      return target == player and player:hasSkill(self)
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local tar = room:askForChoosePlayers(player,table.map(room.alive_players, Util.IdMapper),1,1,"仙授：你可以选择一名角色令其获得技能“天道”，<br>若其已有天道，则改为判定并根据判定结果随机加减“天道”数字",self.name,true)
    if #tar > 0 then
      self.cost_data = tar[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tar = room:getPlayerById(self.cost_data)
    if tar:hasSkill("jlve_tiandao") then
      local mark = type(tar:getMark("@jlve_tiandao")) == "table" and tar:getMark("@jlve_tiandao") or {}
      if #mark <= 0 then
        mark = {1,1,1,1}
      end
      local judge = {
        who = tar,
        reason = self.name,
        pattern = ".",
      }
      room:judge(judge)
      if judge.card.suit == Card.Spade then
        local n = math.random(1,4)
        mark[n] = math.max(mark[n] - 1,0)
      else
        local n = math.random(1,4)
        mark[n] = mark[n] + 1
      end
      room:setPlayerMark(tar,"@jlve_tiandao",mark)
    else
      room:handleAddLoseSkills(tar,"jlve_tiandao",nil,false)
    end
  end,
}
local tiandao = fk.CreateTriggerSkill{
  name = "jlve_tiandao",
  events = {fk.TurnStart},
  frequency = Skill.Compulsory,
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return  target == player and player:hasSkill(self)  
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local mark = type(player:getMark("@jlve_tiandao")) == "table" and player:getMark("@jlve_tiandao") or {}
    if #mark <= 0 then
      mark = {1,1,1,1}
    end
    if mark[1] > 0 then
      player:drawCards(mark[1],self.name)
    end
    if player.dead then return end
    local genn = {}
    local n = mark[2]
    if n > 0 then
    for _, p in ipairs(room.players) do
      table.insertIfNeed(genn,p.general)
      if p.deputyGeneral ~= "" then
          table.insertIfNeed(genn,p.deputyGeneral)
      end
    end
    for i = 1, n, 1 do
      local get = false
      local gp = table.simpleClone(Fk:getAllGenerals())
      table.shuffle(gp)
      for _, general in ipairs(gp) do
        if get then break end
        if (general.kingdom == "qun" or general.subkingdom == "qun") and not table.contains(genn,general.name) then
          local skills = table.simpleClone(general.skills)
          table.shuffle(skills)
          for _, s in ipairs(skills) do
            if get then break end
            if not (table.contains({Skill.Limited, Skill.Wake, Skill.Quest},s.frequency) or s.lordSkill) and not player:hasSkill(s,true) then
              get = true
              room:handleAddLoseSkills(player,s.name,nil,true)
            end
            if get then break end
          end
          if get then break end
        end
      end
    end
    end
    if player.dead then return end
    local m,o = mark[3],mark[4]
    if m > 0 or o > 0 then
      local tar = room:askForChoosePlayers(player,table.map(room.alive_players, Util.IdMapper),1,1,"天道：你可以选择一名角色，随机弃置其 "..m.." 张牌，并对其造成 "..o.." 点雷电伤害。",self.name,true)
      if #tar > 0 then
        tar = room:getPlayerById(tar[1])
        if m > 0 and not tar:isNude() then
          room:throwCard(table.random(tar:getCardIds("he"),m),self.name,tar,player)
        end
        if o > 0 and not tar.dead then
          room:damage({
            from = player,
            to = tar,
            damage = o,
            damageType = fk.ThunderDamage,
            skillName = self.name,
          })
        end
      end
    end

  end,
  refresh_events = {fk.EventAcquireSkill, fk.EventLoseSkill},
  can_refresh = function(self, event, target, player, data)
    if event == fk.EventAcquireSkill or event == fk.EventLoseSkill then
      return target == player and data == self
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventAcquireSkill then
      room:setPlayerMark(player,"@jlve_tiandao",{1,1,1,1})
    elseif event == fk.EventLoseSkill then
      room:setPlayerMark(player,"@jlve_tiandao",0)
    end
  end,
}

local chengfeng = fk.CreateTriggerSkill{
  name = "jlve_chengfeng",
  events = {fk.DamageInflicted,fk.TurnEnd},
  frequency = Skill.Compulsory,
  anim_type = "masochism",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if event == fk.DamageInflicted then
      return  target == player and player:hasSkill(self)  
    elseif event == fk.TurnEnd then
      return target and player:hasSkill(self) and player:getMark("@jlve_chengfeng") >= 2
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DamageInflicted then
      local judge = {
        who = player,
        reason = self.name,
        pattern = ".",
      }
      room:judge(judge)
      if judge.card.suit ~= Card.Spade then
        room:notifySkillInvoked(player,self.name,"defensive")
        player:broadcastSkillInvoke(self.name)
        data.damage = data.damage - 1
      else
        room:notifySkillInvoked(player,self.name,"drawcard")
        player:broadcastSkillInvoke(self.name)
        room:addPlayerMark(player,"@jlve_chengfeng",1)
      end
    elseif event == fk.TurnEnd then
      room:notifySkillInvoked(player,self.name,"masochism")
        player:broadcastSkillInvoke(self.name)
      room:removePlayerMark(player,"@jlve_chengfeng",2)
      player:gainAnExtraTurn(true)
    end
  end,
}
nanhualaoxian:addSkill(xianshou)
nanhualaoxian:addSkill(chengfeng)
nanhualaoxian:addRelatedSkill(tiandao)
Fk:loadTranslationTable{
  ["jlve__nanhualaoxian"] = "南华老仙",
  ["jlve_xianshou"] = "仙授",
  [":jlve_xianshou"] = "你的回合开始时，你可以选择一名角色，若其没有〖天道〗则获得技能〖天道〗，否则其进行一次判定：<br>若结果为♠，令〖天道〗中的随机一项数字-1（不会减至0以下）；<br>若结果不为♠，令〖天道〗中的随机一项数字+1。",
  ["jlve_chengfeng"] = "乘风",
  [":jlve_chengfeng"] = "锁定技，当你受到伤害时进行一次判定：<br>若结果不为♠，此次受到的伤害-1；<br>若结果为♠，你获得一枚“乘风”标记。<br>★一个回合结束时，若你的“乘风”标记不小于两枚，你弃置两枚“乘风”并获得一个额外的回合。",
  ["jlve_tiandao"] = "天道",
  [":jlve_tiandao"] = "锁定技，你的回合开始时，摸1张牌，随机获得一个不在场上的群势力武将牌上的1个技能，然后你可以选择一名角色，随机弃置其1张牌，并对其造成1点雷电伤害。",
  ["@jlve_tiandao"] = "天道",
  ["@jlve_chengfeng"] = "乘风",
}

local qianhuan = fk.CreateTriggerSkill{
  name = "jlve__qianhuan",
  events = {fk.GameStart,fk.TurnStart},
  frequency = Skill.Compulsory,
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if event == fk.GameStart then
      return  player:hasSkill(self)
    elseif event == fk.TurnStart then
      return target == player and player:hasSkill(self)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local gskills = {}
    if type(player:getMark("jlve_qianhuan")) == "table" and #player:getMark("jlve_qianhuan") > 0 then
      local mark = player:getTableMark("jlve_qianhuan")
      room:handleAddLoseSkills(player, "-"..table.concat(mark, "|-"), nil)
      gskills = T.choosemarkskill(player,mark,room,2)
      gskills = gskills or {}
    end
    local gens = {}
    local genn = {}
    for _, p in ipairs(room.players) do
      table.insertIfNeed(genn,p.general)
      if p.deputyGeneral ~= "" then
          table.insertIfNeed(genn,p.deputyGeneral)
      end
    end
    local get = false
    local gp = table.simpleClone(Fk:getAllGenerals())
    table.shuffle(gp)
    for _, g in ipairs(gp) do
      if not table.contains(genn,g.name) then
        table.insertIfNeed(gens,g.name)
      end
      if #gens >= 3 then
        break
      end
    end
    local skills = {}
    local choices = {}
    for _, general_name in ipairs(gens) do
      local general = Fk.generals[general_name]
      local g_skills = {}
      for _, skill in ipairs(general.skills) do
          table.insertIfNeed(g_skills, skill.name)
      end
      for _, s_name in ipairs(general.other_skills) do
        local skill = Fk.skills[s_name]
          table.insertIfNeed(g_skills, skill.name)
      end
      table.insertIfNeed(skills, g_skills)
      if #choices == 0 and #g_skills > 0 then
        choices = {g_skills[1]}
      end
    end
    if #gskills > 0 then
      table.insert(gens,player.general)
      table.insert(skills,gskills)
    end
    if #choices > 0 then
      local result = player.room:askForCustomDialog(player, self.name,
      "packages/jlve/qml/ChooseGeneralSkillsBox.qml", {
        gens, skills, 2, 2, "#jlve__qianhuan-choice", false
      })
      if result ~= "" then
        choices = json.decode(result)
      end
      local mark = {}
      for _, sk in ipairs({choices}) do
        table.forEach(sk, function(s) if not player:hasSkill(s,true) then table.insertIfNeed(mark,s) end end)
      end
      room:setPlayerMark(player,"jlve_qianhuan",mark)
      room:handleAddLoseSkills(player, table.concat(choices, "|"), nil)
    end
  end,
}
Fk:loadTranslationTable{
  ["jlve__qianhuan"] = "千幻",
  [":jlve__qianhuan"] = "锁定技；游戏开始或你的回合开始时，从随机三个不在场上的武将牌的技能中选择两个技能获得（获得技能前你可以将两个此前以此法获得的技能加入技能选项）。",
  ["#jlve__qianhuan-choice"] = "千幻：选择其中两个技能获得",


  ["$jlve__qianhuan1"] = "万物藏身，幻化由心。",
  ["$jlve__qianhuan2"] = "普故纳新，施法天地。",
}
local zuoci = General(extension, "jlve__zuoci","qun",3,3)
zuoci:addSkill(qianhuan)
Fk:loadTranslationTable{
    ["jlve__zuoci"] = "左慈",

    ["~jlve__zuoci"] = "腾云跨风，飞升太虚。",
}


local bolve = fk.CreateTriggerSkill{
  name = "jlve__bolve",
  events = {fk.BeforeTurnStart},
  frequency = Skill.Compulsory,
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if event == fk.BeforeTurnStart then
      return target == player and player:hasSkill(self)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local  ms = type(player:getMark("jlve_bolve")) == "table" and player:getMark("jlve_bolve") or {}
    if #ms > 0 then
      for _, s in ipairs(ms) do
        room:handleAddLoseSkills(player,"-"..s,nil,true)
      end
    end
    room:setPlayerMark(player,"jlve_bolve",0)
    local mark =  type(player:getMark("jlve_renji")) == "table" and player:getMark("jlve_renji") or {}
    table.insertTable(mark,{"shu","wei","wu"})
    if #mark > 0 then
      local mskills = {}
      for _, sl in ipairs(mark) do
        local gp = table.simpleClone(Fk:getAllGenerals())
        table.shuffle(gp)
        local get = false
        for _, general in ipairs(gp) do
          if get then break end
          if (general.kingdom == sl or general.subkingdom == sl)  then
            local skills = table.simpleClone(general.skills)
            table.shuffle(skills)
            for _, s in ipairs(skills) do
              if get then break end
              if not (table.contains({Skill.Limited, Skill.Wake, Skill.Quest},s.frequency) or s.lordSkill) and not player:hasSkill(s,true) then
                get = true
                table.insert(mskills,s.name)
                room:handleAddLoseSkills(player,s.name,nil,true)
              end
              if get then break end
            end
            if get then break end
          end
        end
      end
      room:setPlayerMark(player,"jlve_bolve",mskills)
    end
  end,
}
Fk:loadTranslationTable{
  ["jlve__bolve"] = "博略",
  [":jlve__bolve"] = "锁定技；你的回合开始前，你失去此前以此法获得的技能，然后随机获得“蜀”“魏”“吴”各一个势力武将牌上的一个技能。",

  ["$jlve__bolve1"] = "杀人伎俩，偶尔一用，无防。",
  ["$jlve__bolve2"] = "老夫，想到一件有趣之事。",
  ["$jlve__bolve3"] = "此种事态，老夫，早有准备。",
  ["$jlve__bolve4"] = "无用之物，老夫，毫无兴趣。",
}

local renji = fk.CreateTriggerSkill{
  name = "jlve__renji",
  events = {fk.Damaged},
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
      return target == player and player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(1,self.name)
    if player.dead then return end
    if data.from then
      local mark =  type(player:getMark("jlve_renji")) == "table" and player:getMark("jlve_renji") or {}
      table.insert(mark,data.from.kingdom)
      room:setPlayerMark(player,"jlve_renji",mark)
    end
  end,
}
Fk:loadTranslationTable{
  ["jlve__renji"] = "忍忌",
  [":jlve__renji"] = "当你受到伤害后，你可以摸一张牌，并令你此后发动〖博略〗时额外随机获得一个等同此次伤害来源势力的武将牌的技能。",

  ["$jlve__renji1"] = "休要聒噪，吵得老夫头疼。",
  ["$jlve__renji2"] = "老夫也不得不认真起来了。",
  ["$jlve__renji3"] = "你们..是要置老夫于死地吗？",
}

local biantian = fk.CreateTriggerSkill{
  name = "jlve__biantian",
  events = {fk.EventPhaseStart},
  frequency = Skill.Compulsory,
  anim_type = "negative",
  can_trigger = function(self, event, target, player, data)
      return target and target ~= player and player:hasSkill(self) and target.phase == Player.Judge
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local pattern = ".|2~9|spade"
    local judge = {
      who = target,
      reason = "lightning",
      pattern = pattern,
    }
    room:judge(judge)
    if judge.card:matchPattern(pattern) then
      room:damage{
        to = target,
        damage = 3,
        damageType = fk.ThunderDamage,
        skillName = "lightning_skill",
      }
    end
  end,
}
Fk:loadTranslationTable{
  ["jlve__biantian"] = "变天",
  [":jlve__biantian"] = "锁定技，其他角色的判定阶段开始时，其进行一次【闪电】判定。",

  ["$jlve__biantian"] = "雷击，喝！",
}

local tianyou = fk.CreateTriggerSkill{
  name = "jlve__tianyou",
  events = {fk.TurnEnd},
  frequency = Skill.Compulsory,
  anim_type = "negative",
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    if not (target == player and player:hasSkill(self)) then return false end
    local n = 0 
    local m = 0
    player.room.logic:getActualDamageEvents( 999, function (e)
      local damage = e.data[1]
      if damage.skillName and damage.skillName == "lightning_skill" then
        n = n + 1
      end
    end, Player.HistoryTurn)
    player.room.logic:getActualDamageEvents( 999, function (e)
      local damage = e.data[1]
      if damage.skillName and damage.skillName == "lightning_skill" then
        m = m + 1
      end
    end, Player.HistoryGame)
    self.cost_data = {n,m}
      return target == player  and player:hasSkill(self) and ((n == 0 and player:isWounded()) or (m > 0))
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if self.cost_data[1] == 0 then
      room:recover({
        who = player,
        num = 1,
        recoverBy = player,
        skillName = self.name
      })
    else
      player:drawCards(self.cost_data[2],self.name)
    end
  end,
}
Fk:loadTranslationTable{
  ["jlve__tianyou"] = "天佑",
  [":jlve__tianyou"] = "锁定技，你的回合结束时，若本回合没有角色受到过【闪电】的伤害，你回复一点体力；否则你摸等同于本局游戏所有人受到【闪电】伤害次数张牌。",

  ["$jlve__tianyou"] = "好好看着吧！",
}
local simayi = General(extension, "jlve__mosimayi","jin",3,3)
simayi:addSkill(bolve)
simayi:addSkill(renji)
simayi:addSkill(biantian)
simayi:addSkill(tianyou)
Fk:loadTranslationTable{
    ["jlve__mosimayi"] = "司马懿",

    ["~jlve__mosimayi"] = "啊~",
}

local luocha = fk.CreateTriggerSkill{
  name = "jlve__luocha",
  anim_type = "special",
  priority = 3,
  frequency = Skill.Compulsory,
  events = {fk.GameStart,fk.EnterDying},
  can_trigger = function(self, event, target, player, data)
    if event == fk.GameStart then
      return player:hasSkill(self)
    elseif event == fk.EnterDying then
      return player:hasSkill(self) and target ~= player
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = 1
    if event == fk.GameStart then
      n = 3
    elseif event == fk.EnterDying then
      player:drawCards(2,self.name)
    end
    if player.dead then return end
    local skills = {}
    local gens = Fk:getAllGenerals()
    table.shuffle(gens)
    for _, general in ipairs(gens) do
      for _, skill in ipairs(general.skills) do
        local str = Fk:translate(":"..skill.name)
        if not player:hasSkill(skill,true) and
          (string.find(str, "杀") ) then
          table.insertIfNeed(skills, skill.name)
          break
        end
      end
      if #skills >= n then break end
    end
    if #skills > 0 then
      room:handleAddLoseSkills(player, table.concat(skills, "|"), nil, true, false)
    end
  end,
}
Fk:loadTranslationTable{
  ["jlve__luocha"] = "罗刹",
  [":jlve__luocha"] = "锁定技，游戏开始时，你获得三个技能描述中带有“杀”字的技能；当其他角色濒死时，你摸两张牌并获得一个技能描述带有“杀”字的技能。",

  ["$jlve__luocha1"] = "超越,死亡的恐惧！",
  ["$jlve__luocha2"] = "我，即是不灭！",
}
local shajue = fk.CreateActiveSkill{
  name = "jlve__shajue",
  anim_type = "offensive",
  prompt ="#jlve__shajue",
  can_use = function(self, player)
    return not player:isKongcheng() and not player:prohibitUse(Fk:cloneCard("slash")) and  player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  target_num = 1,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id
  end,
  card_filter  =  Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local to = room:getPlayerById(effect.tos[1])
    local acards = player:getCardIds("h")
    table.shuffle(acards)
    room:loseHp(player,1,self.name)
    if player.dead  then return end
    for _, cid in ipairs(acards) do
      if player.dead or to.dead then break end
      if room:getCardArea(cid) == Card.PlayerHand and room:getCardOwner(cid) == player then
        local card = Fk:getCardById(cid)
        local name = table.random({"thunder__slash","fire__slash"})
        local slash = Fk:cloneCard(name)
        if player:prohibitUse(slash) then break end
        slash.skillName = self.name
        slash:addSubcard(card)
        if U.canUseCardTo(room, player, to,slash, false,false) then
          room:useVirtualCard(name, {card}, player, {to}, self.name, true)
        end
      end
    end
  end,
}
local shajue_trigger = fk.CreateTriggerSkill{
  name = "#jlve__shajue_trigger",
  frequency = Skill.Compulsory,
  priority = 0.1,
  events = { fk.TargetSpecified },
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(shajue) and
      data.card and data.card.trueName == "slash" and table.contains(data.card.skillNames, shajue.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(data.to)
    local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
    if use_event == nil then return end
    room:addPlayerMark(to, fk.MarkArmorNullified)
    use_event:addCleaner(function()
      room:removePlayerMark(to, fk.MarkArmorNullified)
    end)
  end,
}
shajue:addRelatedSkill(shajue_trigger)
Fk:loadTranslationTable{
  ["jlve__shajue"] = "杀绝",
  [":jlve__shajue"] = "出牌阶段限一次，你可以失去一点体力并选择一名角色，然后你将此时的所有手牌随机依次当做无视距离次数和防具的随机属性【杀】对其使用直到你或其死亡。",
  ["#jlve__shajue"] = "失去一点体力将当前所有手牌随机依次当做无视距离次数和防具的随机属性【杀】对其使用",
  ["#jlve__shajue_trigger"] = "杀绝",

  ["$jlve__shajue1"] = "不死不休！",
  ["$jlve__shajue2"] = "这，就是地狱！",
}

local guiqu = fk.CreateTriggerSkill{
  name = "jlve__guiqu",
  events = {fk.EnterDying},
  priority = 1,
  frequency = Skill.Compulsory,
  anim_type = "negative",
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    local n = 0
    for _, s in ipairs(player.player_skills) do
      if not (s.attached_equip or s.name[#s.name] == "&" or string.sub(s.name, 1,1) == "#") then
        n = n + 1
      end
    end
    return target == player and player:hasSkill(self) and n > 0 and player.hp <= 0 and not player:prohibitUse(Fk:cloneCard("peach"))
  end,
  on_cost = function (self,event, target, player, data)
    return player.room:askForSkillInvoke(player,self.name,nil,"鬼躯：你可以失去一个技能并视为使用一张【桃】。")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for i = 1, 999, 1 do
      local skills = {}
      for _, s in ipairs(player.player_skills) do
        if not (s.attached_equip or s.name[#s.name] == "&") then
          table.insert(skills,s.name)
        end
      end
      if #skills < 1 or player.dead then break end
      local gskills = T.choosemarkskillask(player,skills,room,0,1,"#jlve__guiqu")
      if not ( gskills and #gskills >= 1) then break end
      player:broadcastSkillInvoke(self.name)
      room:handleAddLoseSkills(player, "-"..table.concat(gskills, "|-"), nil)
      local peach = Fk:cloneCard "peach"
      peach.skillName = self.name
      room:useCard {
      from = player.id,
      tos = table.map({player.id}, function(pid) return { pid } end),
      card = peach,
      extraUse = true,
      }
      if player.hp > 0 then break end
    end
  end,
}
local guiqu_maxcards = fk.CreateMaxCardsSkill{
  name = "#jlve__guiqu_maxcards",
  fixed_func = function(self, player)
    if player:hasSkill(guiqu) then
      local n = 0
    for _, s in ipairs(player.player_skills) do
      if not (s.attached_equip or s.name[#s.name] == "&" or string.sub(s.name, 1,1) == "#") then
        n = n + 1
      end
    end
      return n
    end
  end,
}
guiqu:addRelatedSkill(guiqu_maxcards)
Fk:loadTranslationTable{
  ["jlve__guiqu"] = "鬼躯",
  [":jlve__guiqu"] = "锁定技，你的手牌上限基值等于你所持技能数；当你进入濒死状态时，可以失去一个技能视为使用一张【桃】。",
  ["jlve__guiqu_maxcards"] = "鬼躯",
  ["#jlve__guiqu"] = "选择失去%arg个技能，然后视为使用一张【桃】",

  ["$jlve__guiqu1"] = "天雷，吾亦不惧！",
  ["$jlve__guiqu2"] = "鬼神之躯，怎能被凡人摧毁！",
}
local splvbu = General(extension, "jlve_sp__godlvbu","god",2,2)
splvbu:addSkill(luocha)
splvbu:addSkill(shajue)
splvbu:addSkill(guiqu)
Fk:loadTranslationTable{
    ["jlve_sp__godlvbu"] = "神吕布",

    ["~jlve_sp__godlvbu"] = "不..我是不朽的！",
}

local kunfen = fk.CreateTriggerSkill{
  name = "jlve__kunfen",
  anim_type = "drawcard",
  priority = 3,
  frequency = Skill.Compulsory,
  events = {fk.Damaged,fk.HpLost,fk.MaxHpChanged},
  can_trigger = function(self, event, target, player, data)
    if event == fk.MaxHpChanged then
      return target == player and player:hasSkill(self) and data.num < 0
    else
      return target == player and player:hasSkill(self)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(3,self.name)
    if player.dead then return end
    if  player:usedSkillTimes(self.name, Player.HistoryTurn) == 1 and player:isWounded() then
      room:recover{
        who = player,
        num = 1,
        recoverBy = player,
        skillName = self.name,
      }
    end
  end,
}
Fk:loadTranslationTable{
  ["jlve__kunfen"] = "困奋",
  [":jlve__kunfen"] = "锁定技，当你受到伤害后、失去体力后、体力上限减少后，你摸3张牌；你每回合首次发动此技能时回复1点体力。",

}

local caiyu = fk.CreateTriggerSkill{
  name = "jlve__caiyu",
  events = {fk.TurnStart},
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return  target == player and player:hasSkill(self)  
  end,
  on_cost = function (self,event, target, player, data)
      return player.room:askForSkillInvoke(player,self.name,nil,"才遇：你可以减少一点体力上限随机获得一个“诸葛亮”的技能。")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:changeMaxHp(player,-1)
    if player.dead then return end
      local get = false
      local gp = table.simpleClone(Fk:getAllGenerals())
      table.shuffle(gp)
      for _, general in ipairs(gp) do
        if get then break end
        if string.find(Fk:translate(general.name), "诸葛亮") then
          local skills = table.simpleClone(general.skills)
          table.shuffle(skills)
          for _, s in ipairs(skills) do
            if get then break end
            if  not player:hasSkill(s,true) then
              get = true
              room:handleAddLoseSkills(player,s.name,nil,true)
            end
            if get then break end
          end
          if get then break end
        end
      end
  end,
}
Fk:loadTranslationTable{
  ["jlve__caiyu"] = "才遇",
  [":jlve__caiyu"] = "你的开始时，你可以减少一点体力上限并随机获得武将名包含“诸葛亮”的武将牌上的一个技能。<br><font color='grey'>村：问就是只有这些“诸葛亮”。</font>",

}
local spjiangwei = General(extension, "jlve_sp__jiangwei","wei",5,5)
spjiangwei:addSkill(kunfen)
spjiangwei:addSkill(caiyu)
Fk:loadTranslationTable{
    ["jlve_sp__jiangwei"] = "姜维",

}

local zhugeliang = General(extension, "jlve_sp__godzhugeliang", "god",7)
local yaozhi = fk.CreateActiveSkill{
  name = "jlve__yaozhi",
  prompt = "妖智：你可摸一张牌，然后从3个出牌阶段可发动的技能中选择一个直到该技能发动或本阶段结束。",
  card_num = 0,
  target_num = 0,
  anim_type = "special",
  card_filter = Util.FalseFunc,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    player:drawCards(1,self.name)
    if player.dead then return end
    local mark = type(player:getMark(self.name)) == "table" and player:getMark(self.name) or {}
    local skills = {}
    local gens = Fk:getAllGenerals()
    table.shuffle(gens)
    for _, general in ipairs(gens) do
      for _, s in ipairs(general.skills) do
        if s:isInstanceOf(ActiveSkill) and not player:hasSkill(s.name,true) and not table.contains(mark,s.name)   then
          table.insertIfNeed(skills, s.name)
          break
        end
      end
      if #skills >= 3 then break end
    end
    if #skills <= 0 then return end
    local choice = room:askForChoice(player,skills,self.name,"妖智：选择一个技能发动。",true)
    table.insertIfNeed(mark,choice)
    room:setPlayerMark(player,self.name,mark)
    room:handleAddLoseSkills(player,choice,nil,false)
    room:askForUseActiveSkill(player,choice,"是否发动技能 "..Fk:translate(choice).." ？",true)
    room:handleAddLoseSkills(player,"-"..choice,nil,false)
  end,
}
local yaozhi_trigger = fk.CreateTriggerSkill{
  name = "#jlve__yaozhi_trigger",
  anim_type = "special",
  events = {fk.EventPhaseStart,fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return target == player and player:hasSkill(self) and  (player.phase == Player.Finish or player.phase == Player.Start)
    elseif event == fk.Damaged then
      return target == player and player:hasSkill(self)
    end
  end,
  on_cost = function (self, event, target, player, data)
    local ev
    if event == fk.EventPhaseStart then
      if player.phase == Player.Finish then
        return player.room:askForSkillInvoke(player,self.name,nil,"妖智：是否摸一张牌，然后从3个结束阶段可发动的技能中选择一个发动？")
      elseif player.phase == Player.Start then
        return player.room:askForSkillInvoke(player,self.name,nil,"妖智：是否摸一张牌，然后从3个准备阶段可发动的技能中选择一个发动？")
      end
    elseif event == fk.Damaged then
      return player.room:askForSkillInvoke(player,self.name,nil,"妖智：是否摸一张牌，然后从3个受到伤害后可发动的技能中选择一个发动？")
    end
    
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(yaozhi.name)
    player:drawCards(1,self.name)
    if player.dead then return end
    local mark = type(player:getMark(yaozhi.name)) == "table" and player:getMark(yaozhi.name) or {}
    if event == fk.EventPhaseStart and player.phase == Player.Finish then
        local skills = {}
        local gens = Fk:getAllGenerals()
        table.shuffle(gens)
        for _, general in ipairs(gens) do
          for _, skill in ipairs(general.skills) do
            local str = Fk:translate(":"..skill.name)
            if not table.contains(mark,skill.name)  and not player:hasSkill(skill.name,true) and
              (string.find(str, "结束阶段") ) then
                if skill:isInstanceOf(TriggerSkill) and type(skill.events) == "table" and table.contains(skill.events,fk.EventPhaseStart) then
                  table.insertIfNeed(skills, skill.name)
                  break
                end
                local get = false
                for _, s in ipairs(skill.related_skills) do
                  if skill:isInstanceOf(TriggerSkill) and type(skill.events) == "table" and table.contains(skill.events,fk.EventPhaseStart) then
                    table.insertIfNeed(skills, skill.name)
                    get = true
                    break
                  end
                end
                if get then
                  break
                end
              break
            end
          end
          if #skills >= 3 then break end
        end
        if #skills <= 0 then return end
        local choice = room:askForChoice(player,skills,self.name,"妖智：选择一个技能发动。",true)
        table.insertIfNeed(mark,choice)
        room:setPlayerMark(player,yaozhi.name,mark)
        local _skill = Fk.skills[choice]
        if not _skill:isInstanceOf(TriggerSkill) then
          _skill = table.find(_skill.related_skills, function (s)
            return s:isInstanceOf(TriggerSkill)
          end)
          if not _skill then return end
        end
        room:handleAddLoseSkills(player,choice,nil,false)
        if _skill:triggerable(event, target, player, data) then
          _skill:trigger(event, target, player, data)
        end
        room:handleAddLoseSkills(player,"-"..choice,nil,false)
      elseif event == fk.EventPhaseStart and player.phase == Player.Start then
        local skills = {}
        local gens = Fk:getAllGenerals()
        table.shuffle(gens)
        for _, general in ipairs(gens) do
          for _, skill in ipairs(general.skills) do
            local str = Fk:translate(":"..skill.name)
            if not table.contains(mark,skill.name)  and not player:hasSkill(skill.name,true) and
              (string.find(str, "准备阶段") ) then
                if skill:isInstanceOf(TriggerSkill) and type(skill.events) == "table" and table.contains(skill.events,fk.EventPhaseStart) then
                  table.insertIfNeed(skills, skill.name)
                  break
                end
                local get = false
                for _, s in ipairs(skill.related_skills) do
                  if skill:isInstanceOf(TriggerSkill) and type(skill.events) == "table" and table.contains(skill.events,fk.EventPhaseStart) then
                    table.insertIfNeed(skills, skill.name)
                    get = true
                    break
                  end
                end
                if get then
                  break
                end
              break
            end
          end
          if #skills >= 3 then break end
        end
        if #skills <= 0 then return end
        local choice = room:askForChoice(player,skills,self.name,"妖智：选择一个技能发动。",true)
        table.insertIfNeed(mark,choice)
        room:setPlayerMark(player,yaozhi.name,mark)
        local _skill = Fk.skills[choice]
        if not _skill:isInstanceOf(TriggerSkill) then
          _skill = table.find(_skill.related_skills, function (s)
            return s:isInstanceOf(TriggerSkill)
          end)
          if not _skill then return end
        end
        room:handleAddLoseSkills(player,choice,nil,false)
        if _skill:triggerable(event, target, player, data) then
          _skill:trigger(event, target, player, data)
        end
        room:handleAddLoseSkills(player,"-"..choice,nil,false)
    elseif event == fk.Damaged then
      local skills = {}
        local gens = Fk:getAllGenerals()
        table.shuffle(gens)
        for _, general in ipairs(gens) do
          for _, skill in ipairs(general.skills) do
            local str = Fk:translate(":"..skill.name)
            if not table.contains(mark,skill.name)  and not player:hasSkill(skill.name,true)  then
                if skill:isInstanceOf(TriggerSkill) and type(skill.events) == "table" and table.contains(skill.events,fk.Damaged) then
                  table.insertIfNeed(skills, skill.name)
                  break
                end
                local get = false
                for _, s in ipairs(skill.related_skills) do
                  if skill:isInstanceOf(TriggerSkill) and type(skill.events) == "table" and table.contains(skill.events,fk.Damaged) then
                    table.insertIfNeed(skills, skill.name)
                    get = true
                    break
                  end
                end
                if get then
                  break
                end
              break
            end
          end
          if #skills >= 3 then break end
        end
        if #skills <= 0 then return end
        local choice = room:askForChoice(player,skills,self.name,"妖智：选择一个技能发动。",true)
        table.insertIfNeed(mark,choice)
        room:setPlayerMark(player,yaozhi.name,mark)
        local _skill = Fk.skills[choice]
        if not _skill:isInstanceOf(TriggerSkill) then
          _skill = table.find(_skill.related_skills, function (s)
            return s:isInstanceOf(TriggerSkill)
          end)
          if not _skill then return end
        end
        room:handleAddLoseSkills(player,choice,nil,false)
        if _skill:triggerable(event, target, player, data) then
          _skill:trigger(event, target, player, data)
        end
        room:handleAddLoseSkills(player,"-"..choice,nil,false)
    end
  end,
}
local xingyun = fk.CreateTriggerSkill{
  name = "jlve_xingyun",
  anim_type = "negative",
  events = {fk.TurnEnd},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:changeMaxHp(player,-1)
    if player.dead then return end
    local mark = type(player:getMark(yaozhi.name)) == "table" and player:getMark(yaozhi.name) or {}
    if #mark <= 0 then return end
    for _, s in ipairs(table.simpleClone(mark)) do
      if player:hasSkill(s,true) then
        table.removeOne(mark,s)
      end
    end
    if #mark <= 0 then return end
    local choice = T.choosemark2skill(player,mark,room,1)[1]
    if not choice then
      choice = table.random(mark)
    end
    room:handleAddLoseSkills(player,choice,nil,false)
  end,
}
yaozhi:addRelatedSkill(yaozhi_trigger)
zhugeliang:addSkill(yaozhi)
zhugeliang:addSkill(xingyun)
Fk:loadTranslationTable{
  ["jlve_sp__godzhugeliang"] = "神诸葛亮",

  ["jlve__yaozhi"] = "妖智",
  ["#jlve__yaozhi_trigger"] = "妖智",
  [":jlve__yaozhi"] = "出牌阶段限一次，或受到伤害后，或准备阶段开始时，或结束阶段开始时，你可以摸一张牌，然后从随机3个对应时机可发动的技能中选择一个发动。",
  ["jlve_xingyun"] = "星陨",
  [":jlve_xingyun"] = "锁定技，你的回合结束时，你减少一点体力上限，然后从〖妖智〗发动过的技能中选择一个获得。",

  ["$jlve__yaozhi1"] = "星辰之力，助我灭敌！",
  ["$jlve__yaozhi2"] = "世间计谋，尽在掌控。",
  ["$jlve_xingyun1"] = "七星不灭，法力不竭！",
  ["$jlve_xingyun2"] = "斗转星移，七星借命！",
  ["~jlve_sp__godzhugeliang"] = "七星灯灭，魂归九州。",


}

return extension